package com.liam.lotterysystem.controller;

import com.liam.lotterysystem.common.errorcode.ControllerErrorCodeConstants;
import com.liam.lotterysystem.common.exception.ControllerException;
import com.liam.lotterysystem.common.pojo.CommonResult;
import com.liam.lotterysystem.common.utils.JacksonUtil;
import com.liam.lotterysystem.controller.param.CreatePrizeParam;
import com.liam.lotterysystem.controller.param.PageParam;
import com.liam.lotterysystem.controller.result.FindPrizeListResult;
import com.liam.lotterysystem.service.PictureService;
import com.liam.lotterysystem.service.PrizeService;
import com.liam.lotterysystem.service.dto.PageListDTO;
import com.liam.lotterysystem.service.dto.PrizeDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.stream.Collectors;

/**
 * @Author: LiamLMK
 * @CreateTime: 2024-10-19
 * @Description:
 * @Version: 1.0
 */
@Tag(name = "奖品功能")
@Slf4j
@RestController
public class PrizeController {

    @Autowired
    private PictureService pictureService;

    @Autowired
    private PrizeService prizeService;

    @Operation(summary = "文件上传")
    @PostMapping(value = "/pic/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    /**
     * bug报告4: 没有添加consumes, 默认传递json格式
     */
    public String uploadPic(
            @RequestPart("file")
            MultipartFile file) {
         return pictureService.savePicture(file);
    }

    /**
     * 创建奖品
     *  RequestPart: 用于接收表单数据 multipart/form-data
     * @param param
     * @param picFile
     * @return
     */
    @Operation(summary = "创建奖品")
    @PostMapping(value = "/prize/create", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public CommonResult<Long> createPrize(@Valid @RequestPart("param") CreatePrizeParam param,
                                          @RequestPart("prizePic") MultipartFile picFile) {
        log.info("createPrize CreatePrizeParam: {}", JacksonUtil.writeValueAsString(param));
        return CommonResult.success(prizeService.createPrize(param, picFile));
    }


    @Operation(summary = "列表翻页")
    @PostMapping("/prize/find-list")
    public CommonResult<FindPrizeListResult> findPrizeList(PageParam param) {
        log.info("findPrizeList PageParam: {}", JacksonUtil.writeValueAsString(param));
        PageListDTO<PrizeDTO> pageListDTO = prizeService.findPrizeList(param);
        return CommonResult.success(convertToFindPrizeListResult(pageListDTO));
    }

    private FindPrizeListResult convertToFindPrizeListResult(PageListDTO<PrizeDTO> pageListDTO) {
        if (pageListDTO == null) {
            throw new ControllerException(ControllerErrorCodeConstants.FIND_PRIZE_LIST_ERROR);
        }

        FindPrizeListResult result = new FindPrizeListResult();
        result.setTotal(pageListDTO.getTotal());
        result.setRecords(
                pageListDTO.getRecords().stream()
                        .map(prizeDTO -> {
                           FindPrizeListResult.PrizeInfo prizeInfo = new FindPrizeListResult.PrizeInfo();
                           prizeInfo.setPrizeName(prizeDTO.getName());
                            prizeInfo.setPrizeId(prizeDTO.getPrizeId());
                            prizeInfo.setDescription(prizeDTO.getDescription());
                            prizeInfo.setImageUrl(prizeDTO.getImageUrl());
                            prizeInfo.setPrice(prizeDTO.getPrice());
                            return prizeInfo;
                        }).collect(Collectors.toList())
        );
        return result;
    }
}
